#include <HttpResponse.h>
#include <utility>

namespace muduo{
    HttpResponse::HttpResponse():_statu(200),_redirect_flag(false){}
    HttpResponse::HttpResponse(int status):_statu(status),_redirect_flag(false){}
    void HttpResponse::Reset(){
        _statu=200;
        _body.clear();
        _redirect_flag=false;
        _redirect_url.clear();
        _header.clear();
    }
    void HttpResponse::SetHeader(const std::string& key, const std::string& val){
        _header.insert(std::make_pair(key, val));
    }
    std::string HttpResponse::GetHeader(const std::string& key){
        auto iter = _header.find(key);
        if(iter == _header.end()){
            return "";
        }
        return iter->second;
    }
    bool HttpResponse::HasHeader(const std::string& key){
        auto iter = _header.find(key);
        if(iter == _header.end()){
            return false;
        }
        return true;
    }
    void HttpResponse::SetContent(const std::string &body, const std::string &type="text/html"){
        _body=body;
        SetHeader("Content-Type", type);
    }
    void HttpResponse::SetRedirect(const std::string &url, int statu=302){
        _redirect_flag=true;
        _redirect_url=url;
        _statu=statu;
    }
    bool HttpResponse::Close(){
        if(HasHeader("Connection") && GetHeader("Connection") == "keep-alive"){
            return false;
        }
        return true;
    }
}